package com.pm.service.file.impl;

import com.pm.entity.po.file.DailyNews;
import com.pm.entity.po.file.FileLib;
import com.pm.entity.po.security.User;
import com.pm.mapper.file.DailyNewsMapper;
import com.pm.mapper.file.FileLibMapper;
import com.pm.service.file.DailyNewsService;
import com.pm.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class DailyNewsServiceImpl extends BaseServiceImpl<DailyNews> implements DailyNewsService {
    @Autowired
    DailyNewsMapper dailyNewsMapper;

    @Autowired
    FileLibMapper fileLibMapper;

    @Resource
    private void setBaseMapper(DailyNewsMapper dailyNewsMapper) {
        super.setBaseMapper(dailyNewsMapper);
    }

    @Override
    public Integer addDailyNews(DailyNews dailyNews, User user) {
        dailyNews.setAddUser(user.getUserId());
        dailyNews.setUpdateUser(user.getUserId());
        Integer returnNum = dailyNewsMapper.add(dailyNews);
        FileLib fileLib = new FileLib();
        if (dailyNews.getFileNames() != null && !dailyNews.getFileNames().isEmpty()) {
            fileLib.setUserId(user.getUserId());
            fileLib.setFiles(dailyNews.getFileNames());
            fileLib.setAimsId(dailyNews.getId());
            fileLib.setType(14);
            fileLib.setFileAddTime(new Date());
            fileLibMapper.add(fileLib);
        }
        return returnNum;
    }

    @Override
    public Integer updateDailyNews(DailyNews dailyNews, User user) {
        dailyNews.setUpdateUser(user.getUserId());
        Integer returnNum = dailyNewsMapper.update(dailyNews);
        FileLib fileLib = new FileLib();
        if (dailyNews.getFileNames() != null && !dailyNews.getFileNames().isEmpty()) {
            fileLib.setUserId(user.getUserId());
            fileLib.setFiles(dailyNews.getFileNames());
            fileLib.setAimsId(dailyNews.getId());
            fileLib.setType(14);
            fileLib.setFileAddTime(new Date());
            fileLibMapper.add(fileLib);
        }
        return returnNum;
    }
}
